<?php

namespace App\Admin\Controllers\Service;

use App\Http\Controllers\Controller;
use App\Models\Company;
use App\Models\Region;
use App\Models\ServiceCity;
use Dcat\Admin\Controllers\AdminController;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Illuminate\Http\Request;
use function foo\func;

class ServiceCityController extends AdminController
{

    protected $title = '开放城市管理';

    /**
     * 列表
     */
    protected function grid()
    {
        $grid = new Grid(new ServiceCity());
        $grid->id;
        $grid->model()->with('default_user_company');
        $grid->model()->with('default_driver_company');
        $grid->column('province_name', '省份');
        $grid->column('city_name', '城市');
        $grid->column('default_user_company.name', '乘客默认公司');
        $grid->column('default_driver_company.name', '司机默认公司');
        $grid->updated_at;
        $grid->created_at;

        $grid->filter(function (Grid\Filter $filter){
            $filter->like('city_name', '城市');
        });

        return $grid;
    }


    /**
     * 表单
     */
    protected function form()
    {
        $form = new Form(new ServiceCity());

        $form->select('province_id', '省份')
            ->options(Region::selectOptionByPid(0))
            ->rules('required|numeric|exists:region,id')
            ->load('city_id', '/tool/region/getCity');

        $form->hidden('province_name')->saving(function () use ($form){
            return Region::query()->find($form->province_id)->name;
        });

        $form->select('city_id', '城市')
            ->options(function ($city_id) use ($form){
                $options = [];
                if($city_id)
                    $options = Region::selectOptionByPid($form->model()->province_id);

                return $options;
            })
            ->rules('required|numeric|exists:region,id|unique:service_city,city_id,{{id}},id,deleted_at,NULL');

        $form->hidden('city_name')->saving(function () use($form) {
            return Region::query()->find($form->city_id)->name;
        });

        $company_options = Company::selectOption();
        $form->select('default_user_company_id', '乘客默认公司')
            ->required()
            ->options($company_options)
            ->saving(function ($id) {
                return intval($id);
            });

        $form->select('default_driver_company_id', '司机默认公司')
            ->required()
            ->options($company_options)
            ->saving(function ($id){
                return intval($id);
            });

        return$form;
    }


    /**
     * 详情
     * @param $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show($id, new ServiceCity());
        $show->id;
        $show->field('province_name', '省份');

        $show->field('city_name', '城市');

        $show->field('default_user_company_id', '用户默认公司')->as(function ($id){
            $company_name = '';
            if($id)
                $company_name = Company::query()->find($id)->name;
            return $company_name;
        });

        $show->field('default_driver_company_id', '司机默认公司')->as(function ($id){
            $company_name = '';
            if($id)
                $company_name = Company::query()->find($id)->name;
            return $company_name;
        });

        $show->updated_at;
        $show->created_at;

        return $show;
    }
}
